<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package      FeatherCMS
 * @subpackage   Authors
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://julien.portalier.com
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

abstract class AuthorModel extends Model
{
	public $module  = null;
	public $recursiveProcess = Q_PROCESS_LIST;
	public $recursiveFields  = 'member_id';
	
	protected $moduleClass;
	protected $old_urlname;
	
	function __construct(Controller $controller=null)
	{
		parent::__construct($controller);
		$this->root = 'files'.DS.$controller->viewPath.DS;
	}
	
	function & getList($groups='author')
	{
		if (!is_array($groups))
			$groups = explode(' ', $groups);
		$conditions = array();
		
		foreach($groups as $i => $grp)
			$conditions['Group.name'][] = trim($grp);
		
		$this->Member->Group->conditions(&$conditions);
		$this->Member->Group->operator('OR');
		$list = $this->Member->Group->listField('member_id', -1);
		
		$authors = array();
		foreach ($list as $a)
			$authors[$a] = $a;
		
		return $authors;
	}
	
	// validation & save
	
	function validates($data=null)
	{
		if (empty($this->data[$this->name]))
		{
			$this->validationErrors[$this->name][''] = _("There must be at least one author.");
			return false;
		}
		
		// urlname
		if (!empty($this->{$this->moduleClass}->id))
			$this->old_urlname = $this->{$this->moduleClass}->field('urlname', null, -1);
		return true;
	}
	
	function __save($type=null)
	{
		if (!$this->beforeSave())
			return false;
		
		// referer
		$module     = $this->{$this->moduleClass};
		$module_key = $module->foreignKey;
		$module_id  = $module->id;
		
		// access rights
		$aco_filespace = "Filer.".str_replace(DS, '/', $this->root.$this->data[$this->moduleClass]['urlname']);
		$aco_resource  = "{$this->moduleClass}.{$this->{$this->moduleClass}->id}";
		
		// gives rights to admins
		foreach(explode(' ', Config::admins) as $grp)
		{
			if (empty($grp))
				continue;
			$this->controller->acl->allow("Group.$grp", $aco_filespace);
			$this->controller->acl->allow("Group.$grp", $aco_resource);
		}
		
		// authors
		$authors = array_unique(explode(',', preg_replace('/\s/', '', $this->data[$this->name]['member_id'])));
		
		// security check: is there an existing account for each author?
		$Member = $this->load->model('Member');
		$Member->_operator = 'OR';
		$Member->process  = Q_PROCESS_LITE;
		$Member->conditions(array('id' => &$authors));
		$authors = $Member->listField('id', -1);
		$this->data[$this->name]['member_id'] = $authors;
		
		if (isset($this->data[$this->name]['select_me']) and $this->data[$this->name]['select_me'])
			array_push($authors, $this->controller->acl->id);
		
		// already defined authors: nothing to do for them
		$this->conditions(array($module_key => $module_id));
		$_authors = $this->listField('member_id', -1);
		
		$new_authors = array_diff($authors, $_authors);
		$del_authors = array_diff($_authors, $authors);
		
		// removing removed authors
		if (!empty($del_authors))
		{
			foreach ($del_authors as $i => $member_id)
			{
				$del_authors[$i] = $this->db->value($member_id);
				$this->controller->acl->destroy($aco_filespace, "User.$member_id");
				$this->controller->acl->destroy($aco_resource,  "User.$member_id");
			}
			
			$conditions = array(
				$module_key => $module_id,
				'member_id' => "-! IN (".implode(', ', $del_authors).")"
			);
			if (!$this->__delete(&$conditions))
				return false;
		}
		
		// adds new authors
		if (!empty($new_authors))
		{
			$values = array();
			foreach($new_authors as $member_id)
			{
				array_push($values, array($module_id, $member_id));
				$this->controller->acl->allow("User.$member_id", $aco_filespace);
				$this->controller->acl->allow("User.$member_id", $aco_resource);
			}
			if (!$this->saveMany(array($module_key, 'member_id'), &$values))
				return false;
		}
		
		return $this->afterSave();
	}
	
	function beforeSave()
	{
		// file space
		if (!empty($this->old_urlname))
		{
			// move it?
			if ($this->old_urlname != $this->data[$this->moduleClass]['urlname'])
			{
				$this->Filer->rename($this->root.$this->old_urlname, $this->root.$this->data[$this->moduleClass]['urlname']);
				$this->controller->acl->destroy("Filer.".str_replace(DS, '/', $this->root.$this->old_urlname));
				return true;
			}
		}
		else
		{
			// create it
			$this->Filer->mkdir($this->root.$this->data[$this->moduleClass]['urlname']);
		}
		return true;
	}
	
	function beforeDelete()
	{
		if (isset($this->deletionIsRecursive) and $this->deletionIsRecursive === true)
		{
			$urlname = $this->{$this->moduleClass}->field('urlname', null, -1);
			$this->Filer->rm($this->root.$urlname);
		}
		return true;
	}
}
?>